package com.atxwq.shop.product.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.atxwq.shop.model.entity.product.Category;
import com.atxwq.shop.product.mapper.CategoryMapper;
import com.atxwq.shop.product.service.CategoryService;
import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 分类业务实现类
 * redisTemplate缓存添加
 * @Date 2025/5/18 13:44
 * @Created by 12336
 */

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public List<Category> selectOneCategory() {
        //从redis中获取
        String categoryOneJson = redisTemplate.opsForValue().get("category:one");
        if(!StringUtils.isEmpty(categoryOneJson)){
            List<Category> categoryList = JSON.parseArray(categoryOneJson, Category.class);
            return categoryList;
        }
        //查询数据库
        List<Category> categoryList = categoryMapper.selectOneCategory();
        //将数据保存到redis中
        redisTemplate.opsForValue().set("category:one",JSON.toJSONString(categoryList));
        return categoryList;
    }

    /**
     *springCache注解缓存添加
     * @return
     */
    @Cacheable(value = "category",key = "'all'")
    @Override
    public List<Category> findCategoryTree() {
        //全部分类
        List<Category> categoryList =  categoryMapper.findAll();

        //遍历所有分类。通过parent_Id为0得到所有一级分类
        List<Category> oneCategoryList = categoryList.stream()
                                        .filter(item -> item.getParentId().longValue() == 0)
                                        .collect(Collectors.toList());
        //遍历所有一级分类，根据parent_Id = id得到所有二级分类
        if(!CollectionUtils.isEmpty(oneCategoryList)){
            oneCategoryList.forEach(oneCategory -> {
                List<Category> twoCategoryList = categoryList.stream()
                        .filter(item -> item.getParentId().longValue() == oneCategory.getId().longValue())
                        .collect(Collectors.toList());
                    //封装二级分类
                    oneCategory.setChildren(twoCategoryList);

                //遍历所有二级分类，根据parent_Id = id得到所有三级分类
                if (!CollectionUtil.isEmpty(twoCategoryList)) {
                    twoCategoryList.forEach(twoCategory -> {
                        List<Category> threeCategoryList = categoryList.stream()
                                .filter(item -> item.getParentId().longValue() == twoCategory.getId().longValue())
                                .collect(Collectors.toList());
                        //封装三级分类
                        twoCategory.setChildren(threeCategoryList);
                    });
                }
            });
        }
        return oneCategoryList;
    }
}
